package com.bocai.eelly.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseArray;
import android.widget.RelativeLayout;

import com.bocai.eelly.R;
import com.bocai.eelly.event.AsyncEvent;
import com.bocai.eelly.event.BackgroundEvent;
import com.bocai.eelly.event.MainEvent;
import com.bocai.eelly.event.PostEvent;
import com.bocai.eelly.net.Api;
import com.bocai.eelly.net.MyRetrofit;
import com.bocai.eelly.views.ExpandTabView;
import com.bocai.eelly.views.ViewMiddle;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.util.ArrayList;
import java.util.LinkedList;

import de.greenrobot.event.EventBus;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;


/**
 * Created by kaizen on 4/1/16.
 */
public class BaseActivity extends RxAppCompatActivity {
    private ViewMiddle viewMiddle;
    ExpandTabView expandTabView;
    RelativeLayout search;
    Observer<String> observer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);

        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_remain);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(0, R.anim.slide_right_out);
    }
    public Api getTestApi(){
        return new MyRetrofit().getGirlApi();
    }
    /**
     *
     * @param context
     * @param title
     * @param groups 第一个列表显示的数据
     * @param children 第二个列表显示的数据
     */
    public void initSearch(final Context context,String title,ArrayList<String> groups,SparseArray<LinkedList<String>> children,Observer<String> observer){
        this.observer=observer;
        viewMiddle = new ViewMiddle(this);
        expandTabView = (ExpandTabView) findViewById(R.id.expandtab_view);
        search = (RelativeLayout) findViewById(R.id.search);
        expandTabView.setValue(title, viewMiddle);
        viewMiddle.updateData(groups, children, this);
        initListener();
//        search.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, SearchNewProductsActivity.class);
//                startActivity(intent);
//            }
//        });
    }
    /**
     * 对列表点击事件进行监听  返回两个连个所选择的position值
     */
    public void initListener() {
        viewMiddle.setOnSelectListener( new ViewMiddle.OnSelectListener() {
            @Override
            public void getValue(String  i1, String  i2) {
                Observable.just(i1+","+i2).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(observer);
                expandTabView.onPressBack();
            }
        });
    }

    /**
     * 在发布线程运行
     * @param event
     */
    public void onEvent(PostEvent event) {
    }

    /**
     * 在主线程运行
     * @param event
     */
    public void onEventMainThread(MainEvent event) {
    }

    /**
     * 如果是主线程发布，则在子线程中运行，否则在发布线程运行
     * @param event
     */
    public void onEventBackgroundThread(BackgroundEvent event) {
    }

    /**
     * 总是新建子线程
     * @param event
     */
    public void onEventAsync(AsyncEvent event) {
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}


